package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.scoreMapper;
import com.example.demo.pojo.Connection;
import com.example.demo.pojo.Dialog;
import com.example.demo.pojo.Score;
import com.example.demo.pojo.User;
import com.example.demo.service.Impl.ConnectionServiceImpl;
import com.example.demo.service.Impl.DialogServiceImpl;
import com.example.demo.util.Unite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class ConnController {

    @Autowired
    ConnectionServiceImpl connectionService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    scoreMapper scoreMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    DialogServiceImpl dialogService;

    //获取某用户的连接
    @GetMapping("/getConnections")
    public Unite getConnections(){
        int user_id=(Integer) request.getSession().getAttribute("user");
        User user=userMapper.getUserById(user_id);
        List<Connection> list=connectionService.getConByReceiveId(user_id);
        //System.out.println(list);
        return new Unite(list,true);
    }

    //用户建立连接
    @GetMapping("/beginCon")
    public Unite beginCon(@RequestParam int conn_id){
        int user_id=(Integer) request.getSession().getAttribute("user");
        try {
            if(connectionService.beginConn(conn_id,user_id))
                return new Unite("建立连接成功",true);
            else{
                return new Unite("不可与此人建立连接",false);
            }
        }catch (Exception e){
            return new Unite(null,false);
        }
    }
    //用户发起连接
    @GetMapping("/insertCon")
    public Unite insertCon(@RequestParam int receiveId){
        int user_id=(Integer) request.getSession().getAttribute("user");
        Connection connection=new Connection();
        connectionService.insertConn(connection,user_id,receiveId);
        return new Unite(null,true);
    }
    //用户结束连接
    @GetMapping("/endCon")
    public Unite endCon(){
        int user_id=(Integer) request.getSession().getAttribute("user");
        connectionService.endConn(user_id);
        return new Unite(null,true);
    }
    @GetMapping("/removeCon")
    public Unite removeCon(@RequestParam int connection_id){
        connectionService.removeCon(connection_id);
        return new Unite(null,true);
    }
//    @GetMapping("/receiveCon")
//    public Unite receiveCon(@RequestParam int connection_id){
//        int user_id=(Integer) request.getSession().getAttribute("user");
//        connectionService.beginConn(connection_id,user_id);
//        return new Unite(null,true);
//    }
    @GetMapping("/getConnectUsers")
    public Unite getConnectUsers(@RequestParam int user_id){
        User user1=userMapper.getUserById(user_id);
        System.out.println(user_id);
        Connection connection=connectionService.getConnection(user_id);
        System.out.println(connection);
        if(connection==null) return new Unite(null,false);
        int temp=(user_id==connection.getSend_user_id())?connection.getReceive_user_id():connection.getSend_user_id();
        User user2=userMapper.getUserById(temp);
        User[] users=new User[2];
        users[0]=user1;users[1]=user2;
//        System.out.println(users[0]);
//        System.out.println(users[1]);
//        System.out.println("---------------------");
        return new Unite(users,true);
    }
    @GetMapping("/insertScore")
    public Unite insertScore(@RequestParam int score){
        Score score1=new Score();
        int user_id=(Integer) request.getSession().getAttribute("user");
        score1.setUser_id(user_id);
        score1.setScore(score);
        scoreMapper.insert(score1);
        return new Unite();
    }
    @GetMapping("getDialogs")
    public Unite getDialogs(){
        int user_id=(Integer) request.getSession().getAttribute("user");
        Connection connection=connectionService.getConnection(user_id);
        if(connection==null) return new Unite(null,false);
        return new Unite(dialogService.getDialogs(connection.getConnection_id()),true);
    }
    @GetMapping("/getAllConnections")
    public Unite getAllConnections(){
        List<Connection> list=connectionService.getAllConnections();
        return new Unite(list,true);
    }
}
